/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * bzr_giveawayanzeigen.java
 *
 * Created on 28.04.2011, 10:11:21
 */
package bzr_lagerverwaltung;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author fhiesl
 */
public class bzr_giveawayanzeigen extends javax.swing.JFrame {

    DefaultTableModel dtm;
    Connection con;

    /** Creates new form bzr_giveawayanzeigen */
    public bzr_giveawayanzeigen() throws InstantiationException, IllegalAccessException, SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://mysql.bezirksrundschau.com:3306/brdbm_1";
            con = (Connection) DriverManager.getConnection(url, "brdbm", "br2011!db");
            initTable();
            initComponents();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(bzr_giveawayanzeigen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(dtm);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 745, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(314, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new bzr_giveawayanzeigen().setVisible(true);
                } catch (InstantiationException ex) {
                    Logger.getLogger(bzr_giveawayanzeigen.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(bzr_giveawayanzeigen.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(bzr_giveawayanzeigen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void initTable() {

        dtm = new DefaultTableModel();
        dtm.addColumn("Name");
        dtm.addColumn("Ist-Stückzahl");
        dtm.addColumn("Mindest-Anzahl");
        dtm.addColumn("Lieferant");
        try {
            Statement st = (Statement) con.createStatement();
            ResultSet rs = (ResultSet) st.executeQuery("SELECT g.Name, g.Ist_Stueckzahl, g.Mindest_Stueckzahl, l.Name FROM GiveAways g, Lieferanten l WHERE g.Lieferanten_L_ID=l.Lieferant_ID");
            String[] row = new String[4];
            while(rs.next()){
                row[0]=rs.getString(1);
                row[1]=""+rs.getInt(2);
                row[2]=""+rs.getInt(3);
                row[3]=rs.getString(4);
                dtm.addRow(row);
            }
        } catch (SQLException ex) {
            Logger.getLogger(bzr_giveawayanzeigen.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
